/****************************************Copyright (c)****************************************************
**                               China Mobile M2M Co., LTD
**
**                                 http://iot.10086.cn/
**
**--------------File Info---------------------------------------------------------------------------------
** File name:           app_fs.c
** Created by:          yangpengbo                  ID:03900265
** Created date:        20161202
** Version:             V1.00
** Descriptions:        creat file
**
**--------------------------------------------------------------------------------------------------------
** Modified by:         x
** Modified date:       x
** Version:             x
** Descriptions:        x
**
** Rechecked by:
*********************************************************************************************************/
#include "include.h"
/*********************************************************************************************************
**                                          MACRO DEF
*********************************************************************************************************/


/*********************************************************************************************************
**                                          ENUM&STRUCT
*********************************************************************************************************/

/*********************************************************************************************************
**                                          GLOBAL VAR
*********************************************************************************************************/

/*********************************************************************************************************
**                                          LOCAL VAR
*********************************************************************************************************/
static  U8  s_fs_gpsdata2_count = 0;
static  U8  s_fs_gpsdata2_buff[GPSDATA2_PACK_FILE_SIZE] = {0};

/*********************************************************************************************************
** Function name:       Fs_Init
** Descriptions:        Fs_Init
** input parameters:    none
** output parameters:   none
** Returned value:      TRUE or FALSE
*********************************************************************************************************/
BOOL    Fs_Init(void)
{
    BOOL ret = FALSE;

	ret = Fs_ReadGpsData2Pack(s_fs_gpsdata2_buff);
   	s_fs_gpsdata2_count = s_fs_gpsdata2_buff[ADDR_GPSDATA2_PACK_COUNT];

    return ret;
}

/*********************************************************************************************************
** Function name:       Fs_ReadGpsData2Pack
** Descriptions:        Fs_ReadGpsData2Pack
** input parameters:    buff
** output parameters:   none
** Returned value:      TRUE or FALSE
*********************************************************************************************************/
BOOL    Fs_ReadGpsData2Pack(U8 * buff)
{
    S32 infoFileHandle;
    S32 fs_result;
    U32 read_size = 0;

    infoFileHandle = iot_fsOpen(GPSDATA2_PACK_FILE_NAME,IOT_FS_CREATE|IOT_FS_READ_WRITE);
    if(infoFileHandle < 0)
    {
        iot_logError("Create Pack file Fail,and Return Error is %d",infoFileHandle);
        return FALSE;
    }

    fs_result = iot_fsRead(infoFileHandle,buff,GPSDATA2_PACK_FILE_SIZE,&read_size);
    if(IOT_FS_NO_ERROR != fs_result || GPSDATA2_PACK_FILE_SIZE !=read_size)
    {
        iot_logDebug("Read Pack file Fail,and Return Error is %d,Readlen is %d",fs_result,read_size);
    }
    iot_fsClose(infoFileHandle);
    return TRUE;
}
/*********************************************************************************************************
** Function name:       Fs_WriteGpsData2Pack
** Descriptions:        Fs_WriteGpsData2Pack
** input parameters:    buff
** output parameters:   none
** Returned value:      TRUE or FALSE
*********************************************************************************************************/
BOOL    Fs_WriteGpsData2Pack(U8 * buff)
{
    S32 infoFileHandle;
    S32 fs_result;
    U32 writeLen;

    infoFileHandle = iot_fsOpen(GPSDATA2_PACK_FILE_NAME,IOT_FS_CREATE|IOT_FS_READ_WRITE);
    if(infoFileHandle < 0)
    {
        iot_logError("Create Pack file Fail,and Return Error is %d",infoFileHandle);
        return FALSE;
    }

    fs_result = iot_fsWrite(infoFileHandle,buff,GPSDATA2_PACK_FILE_SIZE,&writeLen);
    if(IOT_FS_NO_ERROR != fs_result || GPSDATA2_PACK_FILE_SIZE !=writeLen)
    {
        iot_logDebug("Write Pack file Fail,and Return Error is %d,Readlen is %d",fs_result,writeLen);
    }
    iot_fsClose(infoFileHandle);
    return TRUE;
}
/*********************************************************************************************************
** Function name:       GpsData2_GetPackCount
** Descriptions:        GpsData2_GetPackCount
** input parameters:    none
** output parameters:   none
** Returned value:      s_fs_gpsdata2_count
*********************************************************************************************************/
U8      GpsData2_GetPackCount(void)
{
    return s_fs_gpsdata2_count;
}
/*********************************************************************************************************
** Function name:       GpsData2_ReadPack
** Descriptions:        GpsData2_ReadPack
** input parameters:    buff
** output parameters:   none
** Returned value:      TRUE or FALSE
*********************************************************************************************************/
BOOL    GpsData2_ReadPack(U8 * buff)
{
    if(s_fs_gpsdata2_count == 0)
        return FALSE;

    memcpy(buff, (void *)(s_fs_gpsdata2_buff + (s_fs_gpsdata2_count-1)*MAX_GPSDATA2_PACK_LEN), MAX_GPSDATA2_PACK_LEN);

    return TRUE;
}
/*********************************************************************************************************
** Function name:       GpsData2_WritePack
** Descriptions:        GpsData2_WritePack
** input parameters:    buff
** output parameters:   none
** Returned value:      TRUE or FALSE
*********************************************************************************************************/
BOOL    GpsData2_WritePack(U8 * buff)
{
    U8      end_count = 0;
    U8      i;

    if(s_fs_gpsdata2_count == MAX_GPSDATA2_PACK_COUNT)
        end_count = s_fs_gpsdata2_count-1;
    else
        end_count = s_fs_gpsdata2_count;

    for(i = end_count; i>=1; i--)
    {
        memcpy((void *)(s_fs_gpsdata2_buff + i*MAX_GPSDATA2_PACK_LEN), (void *)(s_fs_gpsdata2_buff + (i-1)*MAX_GPSDATA2_PACK_LEN), MAX_GPSDATA2_PACK_LEN);
    }

    memset(s_fs_gpsdata2_buff, 0, MAX_GPSDATA2_PACK_LEN);
    memcpy(s_fs_gpsdata2_buff, buff, MAX_GPSDATA2_PACK_LEN);
	end_count++;
    s_fs_gpsdata2_count = end_count;
	s_fs_gpsdata2_buff[ADDR_GPSDATA2_PACK_COUNT] = s_fs_gpsdata2_count;

    return TRUE;
}
/*********************************************************************************************************
** Function name:       GpsData2_DelPack
** Descriptions:        GpsData2_DelPack
** input parameters:    buff
** output parameters:   none
** Returned value:      TRUE or FALSE
*********************************************************************************************************/
BOOL    GpsData2_DelPack(void)
{
    if(s_fs_gpsdata2_count == 0)
        return FALSE;

    memset((void *)(s_fs_gpsdata2_buff + (s_fs_gpsdata2_count-1)*MAX_GPSDATA2_PACK_LEN), 0, MAX_GPSDATA2_PACK_LEN);
    s_fs_gpsdata2_count--;
    s_fs_gpsdata2_buff[ADDR_GPSDATA2_PACK_COUNT] = s_fs_gpsdata2_count;
	
    if(s_fs_gpsdata2_count == 0)
    {
        Fs_WriteGpsData2Pack(s_fs_gpsdata2_buff);
    }

    return TRUE;
}
/*********************************************************************************************************
** Function name:       GpsData2_SaveOnePack
** Descriptions:        GpsData2_SaveOnePack
** input parameters:    none
** output parameters:   none
** Returned value:      TRUE or FALSE
*********************************************************************************************************/
BOOL	GpsData2_SaveOnePack(void)
{
	U8  buff[MAX_GPSDATA2_PACK_LEN] = {0};
	
	Vdtp_BuildGpsData2FsPack(buff);
	GpsData2_WritePack(buff);

	return TRUE;
}

/*********************************************************************************************************
** Function name:       Fs_SaveData
** Descriptions:        Fs_SaveData
** input parameters:    none
** output parameters:   none
** Returned value:      TRUE or FALSE
*********************************************************************************************************/
BOOL	Fs_SaveData(void)
{
	if(s_fs_gpsdata2_count > 0)
		Fs_WriteGpsData2Pack(s_fs_gpsdata2_buff);

	return TRUE;
}

/*********************************************************************************************************
End Of File
*********************************************************************************************************/

